#ifndef VROTE_HPP_3
#define VROTE_HPP_3
#include "transContentDefine.h"
#include "transContentPortMgr.hpp"


/////////////////////////////////////////////
///Notice: when the function support both timeout and callback, if they are -1 and NULL then will regard you 
///want call the block method.
/////////////////////////////////////////////
class vroute{
	private:
		static vroute* _instance;
		TransContentPortMgr* mPortManager;
		int mState;
	public:
		static vroute* getInstance();
		static void releaseInstance();
		int getState();

		int start();
		int stop();

		int getAPort(int timeout);
		int getAPortSync(getPortCallback gcb);
		
		int registPort(int port, int timeout);
		int registPortSync(int port, registPortCallback rgcb);
		
		int setPortClosingCallback(int port, portClosingCallback pccb);
		portClosingCallback getPortClosingCallback(int port);
		
		int setPortClosingCallbackVid(vroute_t vid, portClosingCallback pccb);
		portClosingCallback getPortClosingCallbackVid(vroute_t vid);
		
		vroute_t bind(int port, int timeout = -1);
		int bind_sync(int port, bindSyncCallback bscb);

		int listen(vroute_t vid, int count);

		int accept(vroute_t vid, int timeout = -1);
		int accept_sync(vroute_t vid, acceptSyncCallback ascb);

		int receive(vroute_t vid, int flag, void* buffer, int bufferLen, int timeout=-1);
		int receive_sync(vroute_t vid, recvSyncCallback rscb);

		int send(vroute_t vid, int flag, const void* buffer, int bufferLen, int timeout = -1);
		int send_sync(vroute_t vid, int flag, const void* buffer, int bufferLen, sendSyncCallback sscb);

		//flag set to ignore_port_error will regist a temp port, or will use inner process udp port
		int sendTo(int localPort, const EndPoint* target, int flag, const void* buffer, int bufferLen, int timeout = -1);
		
		int sendTo_sync(int localPort, const EndPoint* target, int flag, const void* buffer, int bufferLen, sendToSyncCallback stscb);

		int receiveFrom(int localPort, int flag, void* buffer, int bufferLen, int timeout = -1);
		int receiveFrom_sync(int localPort, receiveFromSyncCallback rfscb);
		
		//connect
		int connect(const EndPoint* target, int timeout=-1, int localPort=0);
	
		int connect_sync(const EndPoint* target, connectCallback ccb, int localPort);

		int getData(int port, char* buffer, int len);

		int close_vid(vroute_t vid);

		int close_port(int port);

		int getLocalAddr(NodeAddr* nodeAddr);
		int getRemoteAddr(vroute_t vid, NodeAddr* nodeAddr);

		int tracertTarget(int port, int tracertType, const NodeAddr* target, NodeAddr* nodeList, int nodeCount, int timeout);

		int tracertTarget_sync(int port, int tracertType, const NodeAddr* target, tarcertTargetCallback ttcb);

		int queryTarget(int port, int type, int valueLength, void* value, deviceInfo* result, int timeout);

		int queryTarget_sync(int port, int type, int valueLength, void* value, queryTargetCallback qtcb);

		int targetReachAble(int port, const NodeAddr* target, int timeout);

		int targetReachAble_sync(int port, const NodeAddr* target, targetReachAbleCallback tracb);

		int setFixedNodeList(const NodeAddr* target, const NodeAddr* nodeList, int nodeCount);

		int getVroutePort(vroute_t vid);

		int getErrorInfo(int errno_num, char* buffer, int bufferLen);

		int isPortAlreadyExist(int port);
	private:
		//constructor
		vroute();
		~vroute();


};
#endif //VROTE_HPP_3
